﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using ExtAspNet;
using Ylh.BLL;

public partial class Module_CustServerManger_MyServerCustDetailed : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData(Request.QueryString["CustID"].ToString());
        }

    }

    private void LoadData(string CustID)
    {
        Ylh.BLL.CustBLL bllc = new CustBLL(DbConnectionStr);
        Ylh.Model.TbCust model = bllc.GetModel(Convert.ToInt32(CustID));


        this.CustID.Text = model.CustID.ToString();

        this.CustName.Text = model.CustName;

        this.TelPhone.Text = model.TelPhone;
        this.Industry.Text = model.Industry;

        this.ID.Text = model.ID;
        this.Pwd.Text = model.Pwd;
        this.QQ.Text = model.QQ;
        this.mail.Text = model.mail;

        this.Linkman.Text = model.Linkman;
        this.CompanyAddress.Text = model.CompanyAddress;
        this.From.Text = model.From;

        if (model.RegisterTime != null)
        {
            this.RegisterTime.SelectedDate = Convert.ToDateTime(model.RegisterTime);
        }


        this.Character.Text = model.Character;
        this.Hobby.Text = model.Hobby;
        this.Expect.Text = model.Expect;
        this.Network.Text = model.Network;
        this.advantage.Text = model.advantage;
        this.KeyServer.Text = model.KeyServer;
        LoadServerList(this.CustID.Text);

    }


    private void LoadServerList(string CustID)
    {
        Ylh.BLL.CustServerBLL bllc = new Ylh.BLL.CustServerBLL(DbConnectionStr);
        Grid1.DataSource = bllc.SelectServerInfo(Convert.ToInt32(CustID));

        Grid1.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(CustID.Text))
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference("MyServerCustInfo.aspx?CustID=" + CustID.Text, "添加服务信息"));
        }
        else
        {
            Alert.Show("请先保存客户信息");
        }

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        List<string> li = base.GetCheckID(Grid1);

        if (li.Count == 0)
        {
            Alert.Show("当前没有选中任何记录");
            return;
        }

        Ylh.BLL.CustServerBLL blls = new Ylh.BLL.CustServerBLL(DbConnectionStr);

        blls.DeleteServerInfo(li);

        Alert.Show("删除成功");
    }

    protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
    {
        System.Web.UI.WebControls.LinkButton lbf = (System.Web.UI.WebControls.LinkButton)Grid1.Rows[e.RowIndex].FindControl("UpdateCustServer");

        string str = Grid1.DataKeys[e.RowIndex][0].ToString();

        lbf.OnClientClick = Window1.GetShowReference("MyServerCustInfo.aspx?CustID=" + Request.QueryString["CustID"].ToString() + "&CustServerID=" + str, "修改客户服务信息");

    }


    private Ylh.Model.TbCust GetCustInfo(ref string Sql)
    {
        Ylh.Model.TbCust CustInfo = new Ylh.Model.TbCust();


        //if (!string.IsNullOrEmpty(CustName.Text))
        //{
            CustInfo.CustName = CustName.Text;
            Sql += " CustName=@CustName ,";
        //}
        //if (!string.IsNullOrEmpty(Industry.Text))
        //{
            CustInfo.Industry = Industry.Text;
            Sql += " Industry=@Industry ,";
        //}
        //if (!string.IsNullOrEmpty(Linkman.Text))
        //{
            CustInfo.Linkman = Linkman.Text;
            Sql += " Linkman=@Linkman ,";
        //}
        //if (!string.IsNullOrEmpty(TelPhone.Text))
        //{
            CustInfo.TelPhone = TelPhone.Text;
            Sql += " TelPhone=@TelPhone ,";
        //}
        //if (!string.IsNullOrEmpty(ID.Text))
        //{
            CustInfo.ID = ID.Text;
            Sql += " ID=@ID ,";
        //}
        //if (!string.IsNullOrEmpty(Pwd.Text))
        //{
            CustInfo.Pwd = Pwd.Text;
            Sql += " Pwd=@Pwd ,";
        //}
        //if (!string.IsNullOrEmpty(Linkman.Text))
        //{
            CustInfo.mail = mail.Text;
            Sql += " mail=@mail ,";
        //}
        //if (!string.IsNullOrEmpty(CompanyAddress.Text))
        //{
            CustInfo.CompanyAddress = CompanyAddress.Text;
            Sql += " CompanyAddress=@CompanyAddress ,";
        //}
        //if (!string.IsNullOrEmpty(QQ.Text))
        //{
            CustInfo.QQ = QQ.Text;
            Sql += " QQ=@QQ ,";
        //}
        //if (!string.IsNullOrEmpty(From.Text))
        //{
            CustInfo.From = From.Text;
            Sql += " [From]=@From ,";
        //}
            //if (!string.IsNullOrEmpty(RegisterTime.Text))
            //{
                CustInfo.RegisterTime = Convert.ToDateTime(RegisterTime.Text);
            //}
            //else
            //{
            //    CustInfo.RegisterTime = null;
            //}
            Sql += " RegisterTime=@RegisterTime ,";
        
        //if (!string.IsNullOrEmpty(Character.Text))
        //{
            CustInfo.Character = Character.Text;
            Sql += " [Character]=@Character ,";
        //}
        //if (!string.IsNullOrEmpty(Hobby.Text))
        //{
            CustInfo.Hobby = Hobby.Text;
            Sql += " Hobby=@Hobby ,";
        //}
        //if (!string.IsNullOrEmpty(Expect.Text))
        //{
            CustInfo.Expect = Expect.Text;
            Sql += " Expect=@Expect ,";
        //}
        //if (!string.IsNullOrEmpty(Network.Text))
        //{
            CustInfo.Network = Network.Text;
            Sql += " Network=@Network ,";
        //}
        //if (!string.IsNullOrEmpty(advantage.Text))
        //{
            CustInfo.advantage = advantage.Text;
            Sql += " advantage=@advantage ,";
        //}
        //if (!string.IsNullOrEmpty(KeyServer.Text))
        //{
            CustInfo.KeyServer = KeyServer.Text;
            Sql += " KeyServer=@KeyServer ,";
        //}

        Sql = Sql.Remove(Sql.LastIndexOf(','));
        CustInfo.CustID = Convert.ToInt32(CustID.Text);
        Sql += " where  CustID=@CustID ";

        return CustInfo;
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(RegisterTime.Text))
        {
            Alert.Show("注册时间不能为空");
            return;
        }
        

        string sql = @"Update Tbcust set ";

        Ylh.Model.TbCust custInfo = GetCustInfo(ref sql);

        CustBLL bllc = new CustBLL(DbConnectionStr);

        if (bllc.UpdateCustServerInfo(sql, custInfo))
        {
            Alert.Show("保存成功");
        }


    }

}